/**
 * Created by liubin on 2017/5/2.
 */


var DzpkTableStatus = function() {
    var that = {};

    that.mOldTableStatus = null,

    //牌桌映像
    that.mTableStatus = null;

    //录像msg
    that.replayMsgs = null;

    //录像type
    that.replayTypes = null;

    //是否是断线重连
    that.isReconnect = false;


    //设置牌桌映像
    that.setTableStatus = function(tableStatus){
        that.mTableStatus = tableStatus;

        //记录是否是断线重连
        if (that.mTableStatus.Step != 0){
            that.isReconnect = false;
        }

        //在德州扑克里面才触发
        if (mo.sceneHelper.currentLayerName != "dzpk"){
            return;
        }

        that.updateTableStatus();

        that.mOldTableStatus = tableStatus;
    };

    that.setReconnectStatus = function(tableStatus){
        that.mTableStatus = tableStatus;
        that.mOldTableStatus = tableStatus;

        //记录是否是断线重连
        if (that.mTableStatus.Step == 0){
            that.isReconnect = true;
        }

        //更新用户信息
        that.updateUserInfo();

        //设置当前操作玩家

        //显示自己的吃碰杠胡操作
        that.setTableOperating();

        //更新牌桌step
        mo.dzpkTable.updateTableStep(that.mTableStatus.Step);

        //牌桌重绘
        mo.dzpkTable.mDzpkCardLayer.setReconnectStatus(tableStatus);
    };


    that.updateTableStatus = function(){
        //更新用户信息
        that.updateUserInfo();

        //设置当前操作玩家
        that.setCurrentPlayer();

        //更新动画
        that.updateStep();

        //显示自己的吃碰杠胡操作
        that.setTableOperating();

        //更新牌桌step
        mo.dzpkTable.updateTableStep(that.mTableStatus.Step);

        that.playMusic();
    };


    //更新用户信息
    that.updateUserInfo = function () {
        //牌局结束以后（剩余次数为0，step为0） 不显示用户ui变化
        if (that.mTableStatus.LeftPlayTimes == 0 && that.mTableStatus.Step == 0){
            return;
        }

        if (that.mTableStatus.TableID){
            var infos = [null, null, null, null, null];
            for (var seat = 0; seat < 5; seat++){
                var logicSeat = that.getLogicForView(seat, that.mTableStatus.MySeat - 1);
                infos[seat] = that.mTableStatus["SeatUser" + (logicSeat + 1)];
            }
            mo.dzpkTable.mDzpkUILayer.setTablePlayerInfos(infos);
        }else{
            //录像可能没有
        }
    };

    that.setCurrentPlayer = function () {
        var seat = 0;

        var NowSeat = that.mTableStatus.NowSeat;
        if (NowSeat.length == 0){
            seat = -1;
        }else if (NowSeat.length == 1){
            seat = that.getViewForLogic(NowSeat[0] - 1, that.mTableStatus.MySeat - 1);
        }else{
            seat = -1;
        }

        mo.dzpkTable.setCurrentPlayer(seat);
    },


    that.updateStep = function () {
        if (that.mTableStatus.Step == 0){ //非打牌阶段

        }else if (that.mTableStatus.Step == 1){ //打牌阶段
            //是否起牌
            var isQipai = false;
            if(!that.mTableStatus.LastAction){
                isQipai = true;
                that.mOldTableStatus = that.mTableStatus;

                //得到上局结果
                mo.gameMsgManager.sendGetLastRoundEndRequestdzpk();
            }

            if(isQipai){
                //发牌
                var seats = [false, false, false, false, false];
                var coins = [null, null, null, null, null];
                var cardNums = [[], [], [], [], []];
                var cardTypes = [null, null, null, null, null];

                for (var seat = 0; seat < 5; seat++){
                    var logicSeat = that.getLogicForView(seat, that.mTableStatus.MySeat - 1);
                    var seatuser = that.mTableStatus["SeatUser" + (logicSeat + 1)];
                    if (seatuser != null){
                        seats[seat] = true;
                        coins[seat] = seatuser.PayMoneyOfRound;
                        cardTypes[seat] = seatuser.Cardtype;
                        // if(!seatuser.State){
                            cardTypes[seat] = 10;
                        // }
                        if (seatuser.HandCards.length == 2){
                            cardNums[seat] = seatuser.HandCards;
                        }
                    }
                }

                mo.dzpkTable.fapai(seats, coins, cardNums, cardTypes);

                if(mo.dzpkTable.mGameConfig.men == 1 && that.mTableStatus.Round == 2){
                    mo.dzpkTable.betRoundFinish(1);
                }
                return;
            }

            var nextRound = false;
            if(that.mTableStatus.Round - that.mOldTableStatus.Round == 1 && that.mTableStatus.TableCards.length > 0){
                nextRound = true;
                mo.dzpkTable.betRoundFinish(that.mOldTableStatus.Round);
            }

            //判断在做什么
            var LastAction = that.mTableStatus.LastAction;
            var LastActionSeat = that.mTableStatus.LastActionSeat - 1;

            var LastActionSeatuser = that.mTableStatus["SeatUser" + (LastActionSeat + 1)];
            var OldLastActionSeatuser = that.mOldTableStatus["SeatUser" + (LastActionSeat + 1)];

            LastActionSeat = that.getViewForLogic(LastActionSeat, that.mTableStatus.MySeat - 1);

            var NowSeat = that.mTableStatus.NowSeat - 1;
            NowSeat = that.getViewForLogic(NowSeat, that.mTableStatus.MySeat - 1);

            if (LastAction.code == 0){
                //押注
                if(LastActionSeatuser != null){
                    var thisPayMoney = LastActionSeatuser.PayMoneyOfRound; //当前压住金额
                    if(nextRound){
                        thisPayMoney = LastActionSeatuser.PayMoneyOfLastRound;
                    }
                    var oldPayMomey = OldLastActionSeatuser.PayMoneyOfRound;//上一次压住金额
                    mo.dzpkTable.betChip(LastAction.code, LastActionSeat, thisPayMoney, oldPayMomey);
                }
                mo.dzpkTable.mDzpkUILayer.showActionTip(2, LastActionSeat);

            }else if (LastAction.code == 1){
                //跟注
                if(LastActionSeatuser != null){
                    var thisPayMoney = LastActionSeatuser.PayMoneyOfRound;
                    if(nextRound){
                        thisPayMoney = LastActionSeatuser.PayMoneyOfLastRound;
                    }
                    var oldPayMomey = OldLastActionSeatuser.PayMoneyOfRound;

                    mo.dzpkTable.betChip(LastAction.code, LastActionSeat, thisPayMoney, oldPayMomey);
                }

                mo.dzpkTable.mDzpkUILayer.showActionTip(1, LastActionSeat);

            }else if (LastAction.code == 2){
                //加注
                if(LastActionSeatuser != null){
                    var thisPayMoney = LastActionSeatuser.PayMoneyOfRound;
                    if(nextRound){
                        thisPayMoney = LastActionSeatuser.PayMoneyOfLastRound;
                    }
                    var oldPayMomey = OldLastActionSeatuser.PayMoneyOfRound;

                    mo.dzpkTable.betChip(LastAction.code, LastActionSeat, thisPayMoney, oldPayMomey);
                }
                mo.dzpkTable.mDzpkUILayer.showActionTip(2, LastActionSeat);

            }else if (LastAction.code == 3){
                //让牌
                mo.dzpkTable.mDzpkUILayer.showActionTip(3, LastActionSeat);

            }else if (LastAction.code == 4){
                //弃牌
                mo.dzpkTable.mDzpkUILayer.showActionTip(4, LastActionSeat);

                mo.dzpkTable.mDzpkUILayer.foldState(LastActionSeat, true);
            }else if (LastAction.code == 5){
                //全押
                if(LastActionSeatuser != null){
                    var thisPayMoney = LastActionSeatuser.PayMoneyOfRound;
                    if(nextRound){
                        thisPayMoney = LastActionSeatuser.PayMoneyOfLastRound;
                    }
                    var oldPayMomey = OldLastActionSeatuser.PayMoneyOfRound;

                    mo.dzpkTable.betChip(LastAction.code, LastActionSeat, thisPayMoney, oldPayMomey);
                }

                mo.dzpkTable.mDzpkUILayer.showActionTip(5, LastActionSeat);
            }

            that.checkTableDisplay();
        }
    };

    that.checkTableDisplay = function () {

    };

   that.setTableOperating = function () {
        if(that.mTableStatus.Step != 1){
            return;
        }

        var myUseActionRequest = [];
        var isMyOperating = false;

        for(var i = 0; i < that.mTableStatus.NowSeat.length; i++){
            if (that.mTableStatus.NowSeat[i] == that.mTableStatus.MySeat){
                isMyOperating = true;
                break;
            }
        }

        if (isMyOperating){
            //可以碰杠,单杠不处理
            var seatUser = that.getTableUserStatsdzpk(0);
            for (var i = 0; i < seatUser.NewActionList.length; i++){
                var ActionRequestwhmjItem = seatUser.NewActionList[i];
                var code = ActionRequestwhmjItem.code;
                if (code == 0 || code == 1 || code == 2 || code == 3 || code == 4 || code == 5){
                    myUseActionRequest.push(ActionRequestwhmjItem);
                }
            }
            if (myUseActionRequest.length == 0){
                isMyOperating = false;
            }
        }

        //通知ui
        mo.dzpkTable.mDzpkOprationLayer.showOperating(isMyOperating, myUseActionRequest);
    };

    //返回TableUserStatswhmj
    that.getTableUserStatsdzpk = function(viewSeat){
        var seatUsers = [that.mTableStatus.SeatUser1, that.mTableStatus.SeatUser2, that.mTableStatus.SeatUser3, that.mTableStatus.SeatUser4, that.mTableStatus.SeatUser5];
        var logicSeat = that.getLogicForView(viewSeat, that.mTableStatus.MySeat - 1);

        return seatUsers[logicSeat];
    };

    //通过用户id获取用户性别
    that.getSexForUid = function(uid){
        var sex = 0;
        for (var i = 0; i < 5; i++){
            var seatUser = that.mTableStatus["SeatUser" + (i + 1)];
            if (seatUser != null && seatUser.user_info != null){
                if (seatUser.user_info.userid == uid || seatUser.user_info.nickname == uid){
                    sex = seatUser.user_info.sex;
                }
            }
        }
        return sex;
    };

    //通过视图座位号返回用户性别
    that.getSexForViewSeat = function(view){
        var sex = 0;
        var logicSeat = that.getLogicForView(view, that.mTableStatus.MySeat - 1);
        var seatUser = that.mTableStatus["SeatUser" + (logicSeat + 1)];
        if (seatUser != null && seatUser.user_info != null){
            sex = seatUser.user_info.sex;
        }
        return sex;
    };

    //通过逻辑座位号返回用户性别
    that.getSexForLogicSeat = function(seat){
        var sex = 0;
        var seatUser = that.mTableStatus["SeatUser" + (seat + 1)];
        if (seatUser != null && seatUser.user_info != null){
            sex = seatUser.user_info.sex;
        }
        return sex;
    };

    //返回玩家人数
    that.getPlayerCount = function(){
        var count = 0;
        for (var i = 1; i <=5; i++){
            var userSeat = that.mTableStatus["SeatUser" + i];
            if (userSeat != null){
                count++;
            }
        }
        return count;
    };

    //加载录像
    that.setReplayMsg = function(rePlaydzpk){
        that.replayMsgs = [];
        that.replayTypes = [];

        //设置录像时候自己的座位号
        var replayMySeat = 1;
        var roundEndedEvent = rePlaydzpk.Result;
        for(var i = 1; i <= 5; i++){
            if (roundEndedEvent["SeatUser" + i] != null){
                if (roundEndedEvent["SeatUser" + i].user_info != null){
                    if (roundEndedEvent["SeatUser" + i].user_info.userid == mo.gameUserInfo.uid){
                        replayMySeat = i;
                        break;
                    }
                }
            }
        }
        roundEndedEvent.MySeat = replayMySeat;

        //牌桌映像数据
        for (var i = 0; i < rePlaydzpk.Images.length; i++){
            var tableStatsdzpk = rePlaydzpk.Images[i];
            //修改MySeat
            tableStatsdzpk.MySeat = replayMySeat;

            that.replayMsgs.push(tableStatsdzpk);
            that.replayTypes.push(2306);
        }

        //牌桌结算数据
        that.replayMsgs.push(rePlaydzpk.Result);
        that.replayTypes.push(2308);
    };

    //播放录像
    that.playReplay = function(){
        if (that.replayMsgs.length > 0){
            var msg = that.replayMsgs.shift();
            var type = that.replayTypes.shift();

            var response = null;
            if (type == 2306){
                //牌桌映象
                response = new DzpkTableStatsdzpk();

            }else if (type == 2308){
                //牌桌结算
                response = new DzpkRoundEndedEventdzpk();

            }

            if (response){
                response.handleMsg(msg);
                response.log();
            }else{
                cc.warn("type:" + type + "未注册");
            }
            return true;
        }else{
            cc.log("录像播放完毕");
            return false;
        }

    };

    that.getLogicForView = function(view, myLogicSeat){
        var logic = (view + 5 + myLogicSeat) % 5;
        return logic;
    };

    that.getViewForLogic = function(logic, myLogicSeat){
        var view = (logic + 5 - myLogicSeat) % 5;
        return view;
    };

    //获取自己是否准备
    that.getMyIsReady = function(){
        var seatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
        return seatUser.State == 1;
    };


    //通过ui的返回玩家视图座位号
    that.getViewSeatForUid = function (uid) {
        var logicIndex = -1;
        for (var i = 0; i < 5; i++){
            var seatUser = that.mTableStatus["SeatUser" + (i + 1)];
            if (seatUser != null){
                if (seatUser.user_info.userid == uid || seatUser.user_info.nickname == uid){
                    logicIndex = i;
                }
            }
        }
        if (logicIndex != -1){
            return that.getViewForLogic(logicIndex, that.mTableStatus.MySeat - 1);
        }else{
            return -1;
        }
    };

    //通过用户id获取用户性别
    that.getSexForUid = function(uid){
        var sex = 0;
        for (var i = 0; i < 5; i++){
            var seatUser = that.mTableStatus["SeatUser" + (i + 1)];
            if (seatUser != null && seatUser.user_info != null){
                if (seatUser.user_info.userid == uid || seatUser.user_info.nickname == uid){
                    sex = seatUser.user_info.sex;
                }
            }
        }
        return sex;
    };

    //通过视图座位号返回用户性别
    that.getSexForViewSeat = function(view){
        var sex = 0;
        var logicSeat = that.getLogicForView(view, that.mTableStatus.MySeat - 1);
        var seatUser = that.mTableStatus["SeatUser" + (logicSeat + 1)];
        if (seatUser != null && seatUser.user_info != null){
            sex = seatUser.user_info.sex;
        }
        return sex;
    };

    //通过逻辑座位号返回用户性别
    that.getSexForLogicSeat = function(seat){
        var sex = 0;
        var seatUser = that.mTableStatus["SeatUser" + (seat + 1)];
        if (seatUser != null && seatUser.user_info != null){
            sex = seatUser.user_info.sex;
        }
        return sex;
    };

    //返回玩家人数
    that.getPlayerCount = function(){
        var count = 0;
        for (var i = 1; i <=5; i++){
            var userSeat = that.mTableStatus["SeatUser" + i];
            if (userSeat != null){
                count++;
            }
        }
        return count;
    };

    //获取玩家同ip信息
    that.getPlayerIpEqual = function(){
        var result = [];

        //记录ip
        var ipStruct = {};
        for (var i = 1; i <= 5; i++){
            var seatUser = that.mTableStatus["SeatUser" + i];
            if (seatUser != null && seatUser.user_info != null){
                var userInfo = seatUser.user_info;
                if (mo.gameHelper.getIsJson(userInfo.DeviceJson)) {
                    var deviceJson = JSON.parse(userInfo.DeviceJson);
                    var ip = deviceJson.ip;
                    if (ip != null && ip != "" && ip != "127.0.0.1") {
                        if (ipStruct[ip] == undefined) {
                            ipStruct[ip] = [];
                        }
                        ipStruct[ip].push(userInfo.nickname);
                    }
                }
            }
        }

        //分解出出现两次的用户
        for(var key in ipStruct){
            if (ipStruct.hasOwnProperty(key)) {
                if (ipStruct[key].length > 1){
                    var nicks = ipStruct[key];
                    for (var i = 0; i < nicks.length; i++){
                        var item = {};
                        item.nick = nicks[i];
                        item.ip = key;
                        result.push(item);
                    }
                }
            }
        }

        return result;
    };


    //播放音效
    that.playMusic = function(){
        var LastAction = that.mTableStatus.LastAction;
        if (LastAction == null){
            return;
        }
        var sex = 0;
        if (that.mTableStatus["SeatUser" + that.mTableStatus.LastActionSeat] != null && that.mTableStatus["SeatUser" + that.mTableStatus.LastActionSeat].user_info != null){
            sex = that.mTableStatus["SeatUser" + that.mTableStatus.LastActionSeat].user_info.sex;
        }

        if (that.mTableStatus.Step == 1){
            if(that.mTableStatus.Round == 1 && !that.mTableStatus.LastAction){
                mo.soundHelper.playDzpkDealCard();
            }else if(LastAction.code == 1){
                mo.soundHelper.playDzpkCall(sex);
            }else if(LastAction.code == 2 || LastAction.code == 0){
                mo.soundHelper.playDzpkRaise(sex);
            }else if(LastAction.code == 3){
                mo.soundHelper.playDzpkCheck(sex);
            }else if(LastAction.code == 4){
                mo.soundHelper.playDzpkFold(sex);
            }else if(LastAction.code == 5){
                mo.soundHelper.playDzpkAllin(sex);
            }
        }
    };


    return that;
}

mo.dzpkTableStatus = new DzpkTableStatus();